package GroupName.Project_1;

import java.awt.EventQueue;
import java.awt.Image;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.awt.event.ActionEvent;
import javax.swing.JFormattedTextField;
import javax.swing.JSpinner;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JSeparator;
import javax.swing.JProgressBar;
import javax.swing.JTextPane;

import GroupName.Project_1.Configuration;
import GroupName.Project_1.Configuration.Builder;

import javax.swing.JScrollBar;
import java.awt.List;
import java.awt.Toolkit;

import javax.swing.JTabbedPane;
import javax.swing.JList;
import javax.swing.JTextArea;
import java.awt.Color;
import java.awt.Container;

import javax.swing.ImageIcon;  
import javax.swing.JFrame;  
import javax.swing.JLabel;  
import javax.swing.JPanel; 
public class MainUi2 {

	JFrame frame1;
	@SuppressWarnings("unused")
	//private Process p;
	private JTextField textField_3;
	private JTextField screatvideoField;
	private JTextField textField_1;
	JProgressBar progressBar_1 = new JProgressBar();
	
	/**
	 * Launch the application.
	 */
	

	/**
	 * Create the application.
	 */
	public MainUi2() {
		
		initialize();
	}
	@SuppressWarnings("null")
	public String generateEncodeArg() {
		Configuration config = null;
		return "-d "+config.getEncodeCfgFileAbsPath()+" "+config.getSecertFileAbsPath();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		final JTextField videopathtextField = new JTextField();
		final JTextField screattextField_1 = new JTextField();
		final JTextField textField_2 = new JTextField();
		
		frame1 = new JFrame();
		frame1.setBounds(100, 100, 392, 457);
		frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame1.getContentPane().setLayout(null);	
		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 384, 429);
		
		frame1.getContentPane().add(panel);
		panel.setLayout(null);
		frame1.setResizable(false);
		Toolkit tk=Toolkit.getDefaultToolkit();  
		Image img=tk.getImage("\\exe\\title.jpg"); // GIF、JPEG 或 PNG  
		frame1.setIconImage(img);
		frame1.setTitle("视频水印功能");
		JLabel lblNewLabel = new JLabel("嵌入提取界面");
		lblNewLabel.setBounds(154, 0, 169, 29);
		panel.add(lblNewLabel);
		JLabel label1 = new JLabel("选择原始视频:");
		label1.setBounds(8, 34, 86, 23);
		panel.add(label1);
		videopathtextField.setBounds(96, 34, 253, 23);
		panel.add(videopathtextField);
		videopathtextField.setColumns(10);
		Icon login = new ImageIcon("timg.png");
		JButton btnNewButton = new JButton();
		btnNewButton.setIcon(new ImageIcon(".\\exe\\timg_1.png"));
		btnNewButton.addActionListener(new ActionListener() {
			@SuppressWarnings("unused")
			public void actionPerformed(ActionEvent e)
			{
				EventQueue.invokeLater(new Runnable() {
					public void run() {
						try {
							JFileChooser jfc=new JFileChooser();  
					        jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
					        jfc.showDialog(new JLabel(), "选择");  
					        File file=jfc.getSelectedFile();
							//File cf = jfc.getSelectedFile();
							    System.out.println(jfc.getSelectedFile().getName());  
								String newTxt=null;
								newTxt=file.getAbsolutePath();
								videopathtextField.setText(newTxt);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
				
			}
		});	
		btnNewButton.setBounds(355, 34, 20, 23);
		panel.add(btnNewButton);
		JLabel lblNewLabel_1 = new JLabel("是否进行格式转化：");
		lblNewLabel_1.setBounds(8, 196, 356, 29);
		panel.add(lblNewLabel_1);
		JButton button = new JButton("进行格式转化");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				 new Thread(){  
                     public void run(){  
                    	 Runtime mt =Runtime.getRuntime();
                    	 File  myfile =new File(".\\exe\\FormatFactory","FormatFactory.exe");
                    	 try {
							mt.exec(myfile.getAbsolutePath());
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
			            }  
			        }.start();	
			}
		});
		button.setBounds(8, 226, 112, 29);
		panel.add(button);	
		JButton button_1 = new JButton("返回前页");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
				frame1.setVisible(false);
				MainUi1 mu=new MainUi1();
				mu.setVisible(true);
				//mu.frame1.setDefaultCloseOperation(0);
			}
		});
		button_1.setBounds(259, 226, 105, 29);
		panel.add(button_1);	
		JLabel label_1 = new JLabel("选择水印文件:");
		label_1.setBounds(8, 69, 86, 23);
		panel.add(label_1);	
		
		screattextField_1.setColumns(10);
		screattextField_1.setBounds(96, 69, 253, 23);
		panel.add(screattextField_1);	
		JButton button_2 = new JButton("");
		button_2.setIcon(new ImageIcon(".\\exe\\timg_1.png"));
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				EventQueue.invokeLater(new Runnable() {
					public void run() {
						 new Thread(){  
	                            public void run(){  
	                            	try {
	        							JFileChooser jfc=new JFileChooser();  
	        					        jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
	        					        jfc.showDialog(new JLabel(), "选择");  
	        					        File file=jfc.getSelectedFile();
	        							//File cf = jfc.getSelectedFile();
	        							 if(file.isDirectory()){  
	        						            System.out.println("文件夹:"+file.getAbsolutePath());  
	        						        }else if(file.isFile()){  
	        						            System.out.println("文件:"+file.getAbsolutePath());  
	        						        } 
	        							 
	        						        System.out.println(jfc.getSelectedFile().getName());  
	        								String newTxt=null;
	        								newTxt=file.getAbsolutePath();
	        								screattextField_1.setText(newTxt);
	        						} catch (Exception e) {
	        							e.printStackTrace();
	        						}
					            }  
					        }.start();
						
					}
				});
				
			}
		});
		final JProgressBar progressBar = new JProgressBar();
		progressBar.setBounds(8, 174, 356, 23);
		progressBar.setStringPainted(true);// 设置进度条上的字符串显示，false则不能显示 
		panel.add(progressBar);
		button_2.setBounds(355, 67, 20, 23);
		panel.add(button_2);
		JButton button_3 = new JButton("嵌入水印");	
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Thread(new Runnable(){
					public void run() {
						Process p;
						String videoPath = videopathtextField.getText();
						String secertPath = screattextField_1.getText();
						String outfilesecertvideoPath=screatvideoField.getText();
						String cmd = String.format("%s %s %s %s", "exe/self/robust.exe emb", videoPath, secertPath, outfilesecertvideoPath);
						try {
							p = Runtime.getRuntime().exec(cmd, null, new File("exe/self/"));
							BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));
							String line = null;
							while ((line = stdout.readLine()) != null) {
								
								final String li = line;
								EventQueue.invokeLater(new Runnable() {
									public void run() {
										//console.append(li+"\n");
									}
								});
								
							System.out.println(line);
							}
							stdout.close();
						} catch (IOException e) {e.printStackTrace();}
					}
				}).run();
			}
	});
				/*
				EventQueue.invokeLater(new Runnable() {
					public void run() {
						new Thread(){  
	                            public void run(){  
					                for (int i = 0; i < 101; i++) {  
					                    try {  
					                        Thread.sleep(100);  //   让当前线程休眠0.1ms  
					                    } catch (InterruptedException e) {  
					                        // TODO: handle exception  
					                        e.printStackTrace();  
					                    }  
					                    progressBar.setValue(i); // 设置进度条数值  
					                }  
					               // progressBar.setString("嵌入完成");// 设置提示信息  
					            }  
					        }.start();
					    	private Process p;
							p = Runtime.getRuntime().exec(exePath+" "+arg, null, new File(config.getExecEnvAbsPath()));
						*/
		
		button_3.setBounds(96, 135, 188, 29);
		panel.add(button_3);	
		JLabel label_2 = new JLabel("嵌入进度：");
		label_2.setBounds(8, 149, 78, 23);
		panel.add(label_2);	
		JLabel label_3 = new JLabel("选择水印视频:");
		label_3.setBounds(8, 265, 86, 23);
		panel.add(label_3);
		textField_2.setColumns(10);
		textField_2.setBounds(96, 265, 253, 23);
		panel.add(textField_2);
		JButton button_5 = new JButton("");
		button_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				EventQueue.invokeLater(new Runnable() {
					public void run() {
						 new Thread(){  
	                            public void run(){  
	                            	try {
	        							JFileChooser jfc=new JFileChooser();  
	        					        jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
	        					        jfc.showDialog(new JLabel(), "选择");  
	        					        File file=jfc.getSelectedFile();
	        							//File cf = jfc.getSelectedFile();
	        							 if(file.isDirectory()){  
	        						            System.out.println("文件夹:"+file.getAbsolutePath());  
	        						        }else if(file.isFile()){  
	        						            System.out.println("文件:"+file.getAbsolutePath());  
	        						        } 
	        							 
	        						        System.out.println(jfc.getSelectedFile().getName());  
	        								String newTxt=null;
	        								newTxt=file.getAbsolutePath();
	        								textField_2.setText(newTxt);
	        						} catch (Exception e) {
	        							e.printStackTrace();
	        						}
					            }  
					        }.start();
						
					}
				});
			}
		});
		button_5.setIcon(new ImageIcon(".\\exe\\timg_1.png"));
		button_5.setBounds(355, 265, 20, 23);
		panel.add(button_5);
		JLabel label_4 = new JLabel("提取进度：");
		label_4.setBounds(8, 363, 78, 23);
		panel.add(label_4);
		progressBar_1.setStringPainted(true);// 设置进度条上的字符串显示，false则不能显示 
		progressBar_1.setBounds(8, 396, 356, 23);
		
		JButton button_4 = new JButton("提取水印");
		
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new Thread(new Runnable() {
					public void run() {
						Process p;
						String videoPath = textField_2.getText();
						String bitLength = textField_3.getText();
						String outsecret = textField_1.getText();
						String cmd = String.format("%s %s %s %s", "exe/self/robust.exe ext", videoPath,outsecret, bitLength);
						try {
							p = Runtime.getRuntime().exec(cmd, null, new File("exe/self/"));
							BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));
							String line = null;
							int lineCnt = 0;
							while ((line = stdout.readLine()) != null) {
								System.out.println(line);
								final int count = lineCnt++;
								EventQueue.invokeLater(new Runnable() {
									public void run() {
										System.out.println(progressBar_1.getValue());
										progressBar_1.setMaximum(300);
										progressBar_1.setValue(count);
									}
								});
							}
							stdout.close();
						} catch (IOException e) {e.printStackTrace();}
					}
				}).start();
			}
		});
		/*
		button_4.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				EventQueue.invokeLater(new Runnable() {
					public void run() {
						 new Thread(){  
	                            public void run(){  
					                for (int i = 0; i < 101; i++) {  
					                    try {  
					                        Thread.sleep(100);  //   让当前线程休眠0.1ms  
					                    } catch (InterruptedException e) {  
					                        // TODO: handle exception  
					                        e.printStackTrace();  
					                    }  
					                    progressBar_1.setValue(i); // 设置进度条数值  
					                }  
					                //progressBar_1.setString("提取完成");// 设置提示信息  
					            }  
					        }.start(); //  启动进度条线程 
					}
				});
			}
		});
		*/
		button_4.setBounds(214, 326, 158, 29);
		panel.add(button_4);
		JPanel panel_1 = new JPanel();
		JPanel panel_2 = new JPanel();
		panel_2.setBounds(10, 273, 209, 25);
		panel_1.add(panel_2);
		panel_2.setLayout(null);
		JButton button_7 = new JButton("提取水印");
		panel.add(button_7);
		
		JLabel lblNewLabel_2 = new JLabel("提取字节数：");
		lblNewLabel_2.setBounds(8, 328, 75, 25);
		panel.add(lblNewLabel_2);
		textField_3 = new JTextField();
		textField_3.setBounds(93, 328, 66, 25);
		panel.add(textField_3);
		textField_3.setColumns(10);
		JLabel label = new JLabel("bit");
		label.setBounds(171, 328, 33, 25);
		panel.add(label);
		JLabel outscreatvideoNewLabel = new JLabel("水印视频路径:");
		outscreatvideoNewLabel.setBounds(8, 102, 84, 23);
		panel.add(outscreatvideoNewLabel);
		screatvideoField = new JTextField();
		screatvideoField.setColumns(10);
		screatvideoField.setBounds(96, 102, 253, 23);
		panel.add(screatvideoField);
		JLabel label_5 = new JLabel("提取后水印文件:");
		label_5.setBounds(8, 298, 90, 23);
		panel.add(label_5);
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(96, 298, 253, 23);
		panel.add(textField_1);
		JButton button_6 = new JButton("");
		button_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser jfc=new JFileChooser();  
		        jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
		        jfc.showDialog(new JLabel(), "选择");  
		        File file=jfc.getSelectedFile();
				//File cf = jfc.getSelectedFile();
				 if(file.isDirectory()){  
			            System.out.println("文件夹:"+file.getAbsolutePath());  
			        }else if(file.isFile()){  
			            System.out.println("文件:"+file.getAbsolutePath());  
			        } 
				 
			        System.out.println(jfc.getSelectedFile().getName());  
					String newTxt=null;
					newTxt=file.getAbsolutePath();
					screatvideoField.setText(newTxt);
		
			}
		});
		button_6.setBounds(355, 102, 20, 23);
		panel.add(button_6);
		JButton button_8 = new JButton("");
		button_8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser jfc=new JFileChooser();  
		        jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
		        jfc.showDialog(new JLabel(), "选择");  
		        File file=jfc.getSelectedFile();
				//File cf = jfc.getSelectedFile();
				 if(file.isDirectory()){  
			            System.out.println("文件夹:"+file.getAbsolutePath());  
			        }else if(file.isFile()){  
			            System.out.println("文件:"+file.getAbsolutePath());  
			        } 
				 
			        System.out.println(jfc.getSelectedFile().getName());  
					String newTxt=null;
					newTxt=file.getAbsolutePath();
					textField_1.setText(newTxt);
			}
		});
		button_8.setBounds(355, 298, 20, 23);
		panel.add(button_8);
		panel.add(progressBar_1);
		
	}
}
